package com.demo.controller;

import com.alibaba.fastjson.JSON;
import com.demo.pojo.Msg;
//import com.demo.sdk.CCPRestSDK;
import com.demo.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
public class HomeController {
    @Autowired
    MsgService msgService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    Random random=new Random();
    int nextInt = random.nextInt(1000000);
    String str=String.valueOf(nextInt);
    @RequestMapping("add")
    public String add(Msg msg){
        int add = msgService.add(msg);
        stringRedisTemplate.delete(str);
        return JSON.toJSONString(add);
    }
    @RequestMapping("random")
    public String random(String phone){
        String s;
        stringRedisTemplate.opsForValue().set(str,str,60, TimeUnit.SECONDS);
        try{
            SDKTestSendTemplateSMS sdkTestSendTemplateSMS=new SDKTestSendTemplateSMS();
            sdkTestSendTemplateSMS.show(phone,str);
            s="";
        }catch (Exception e){
            s="服务器内部错误";
        }
        return JSON.toJSONString(s);
    }
    @RequestMapping("chemsg")
    public String chemsg(String msg){
        String str = stringRedisTemplate.opsForValue().get(msg);
        String s;
        if(str!=null){
            return JSON.toJSONString("验证码正确");
        }else{
            return JSON.toJSONString("验证码错误");
        }
    }
}
